package com.qiwx.pigeon.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.data.CageStatusConfig;
import com.qiwx.pigeon.data.params.FertilizeParam;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @description
 * @Author qiwx
 * @Date 2020-05-19 11:04
 * 查蛋optionsview
 **/
public class CheckEggOptionView extends LinearLayout {
    @BindView(R.id.rb_egg2)
    RadioButton rbEgg2;
    @BindView(R.id.rb_egg1)
    RadioButton rbEgg1;
    @BindView(R.id.rb_wudan)
    RadioButton rbWudan;
    @BindView(R.id.rb_egg_cpd)
    RadioButton rbEggCpd;
    @BindView(R.id.rb_bfd)
    RadioButton rbBfd;
    @BindView(R.id.group_check_egg_1)
    RadioGroup groupCheckEgg1;
    @BindView(R.id.rb_shouj2)
    RadioButton rbShouj2;
    @BindView(R.id.rb_sij2)
    RadioButton rbSij2;
    @BindView(R.id.rb_wuj2)
    RadioButton rbWuj2;
    @BindView(R.id.rb_shou2wuj1)
    RadioButton rbShou2wuj1;
    @BindView(R.id.rb_shou2sij1)
    RadioButton rbShou2sij1;
    @BindView(R.id.rb_wu2sij1)
    RadioButton rbWu2sij1;
    @BindView(R.id.group_fertilize2)
    GridRadioGroup groupFertilize2;
    @BindView(R.id.rb_shouj1)
    RadioButton rbShouj1;
    @BindView(R.id.rb_wujing1)
    RadioButton rbWujing1;
    @BindView(R.id.rb_sijing1)
    RadioButton rbSijing1;
    @BindView(R.id.group_fertilize1)
    RadioGroup groupFertilize1;
    @BindView(R.id.rb_cd_fd)
    RadioButton rbCdFd;
    @BindView(R.id.rb_cd_empty)
    RadioButton rbCdEmpty;
    @BindView(R.id.group_cage_status)
    RadioGroup groupCageStatus;
    @BindView(R.id.lin_group_options_checkegg)
    LinearLayout linGroupOptionsCheckegg;


    public CheckEggOptionView(Context context) {
        this(context, null);
    }

    public CheckEggOptionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CheckEggOptionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initEvent();
    }

    private void initView(Context context) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = mInflater.inflate(R.layout.group_check_egg_layout, null);
        ButterKnife.bind(this, view);
        addView(view);
    }

    public void initEvent() {
        rbWudan.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                rbCdEmpty.setChecked(true);
                groupFertilize2.setVisibility(View.GONE);
                groupFertilize1.setVisibility(View.GONE);

            }

        });
        rbEggCpd.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                rbCdEmpty.setChecked(true);
                groupFertilize2.setVisibility(View.GONE);
                groupFertilize1.setVisibility(View.GONE);
            }
        });
        rbBfd.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                rbCdEmpty.setChecked(true);
                groupFertilize2.setVisibility(View.GONE);
                groupFertilize1.setVisibility(View.GONE);
            }
        });
        rbEgg1.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                rbCdFd.setChecked(true);
                rbShouj1.setChecked(true);
                groupFertilize2.setVisibility(View.GONE);
                groupFertilize1.setVisibility(View.VISIBLE);

            }
        });
        rbEgg2.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                rbCdFd.setChecked(true);
                rbShouj2.setChecked(true);
                groupFertilize2.setVisibility(View.VISIBLE);
                groupFertilize1.setVisibility(View.GONE);
            }
        });
        rbEgg2.setChecked(true);
    }

    //当前鸽笼还剩几个蛋
    public FertilizeParam getParam(int amount) {
        //用户选择两个下蛋
        FertilizeParam param = new FertilizeParam();
        int eggStatus = 0;
        if (rbEgg2.isChecked()) {
            if (rbShouj2.isChecked()) {
                eggStatus = CageStatusConfig.CHECK_EGG_SHOUJING2;
            } else if (rbWuj2.isChecked()) {
                eggStatus = CageStatusConfig.CHECK_EGG_WUJING2;
            } else if (rbSij2.isChecked()) {
                eggStatus = CageStatusConfig.CHECK_EGG_SIJING2;
            } else if (rbShou2sij1.isChecked()) {
                eggStatus = CageStatusConfig.CHECK_EGG_SHOUJING2SIJING1;
            } else if (rbShou2wuj1.isChecked()) {
                eggStatus = CageStatusConfig.CHECK_EGG_SHOUJING2WUJING1;
            } else if (rbWu2sij1.isChecked()) {
                eggStatus = CageStatusConfig.CHECK_EGG_WUJING2SIJING1;
            }
        } else if (rbEgg1.isChecked()) {
            if (rbShouj1.isChecked()) {
                eggStatus = CageStatusConfig.CHECK_EGG_SHOUJING1;
            } else if (rbWujing1.isChecked()) {
                eggStatus = CageStatusConfig.CHECK_EGG_WUJING1;
            } else if (rbSijing1.isChecked()) {
                eggStatus = CageStatusConfig.CHECK_EGG_SIJING1;
            }
        } else if (rbWudan.isChecked()) {
            eggStatus = CageStatusConfig.CHECK_EGG_WUDAN;
            param.setWudan(amount);

        } else if (rbBfd.isChecked()) {
            eggStatus = CageStatusConfig.CHECK_EGG_BUFUDAN;
            param.setBfd(amount);

        } else if (rbEggCpd.isChecked()) {
            eggStatus = CageStatusConfig.CHECK_EGG_BROKEN;
            param.setBreaken(amount);
        }
        param.setAmount(amount);
        param.setFudan(!rbCdEmpty.isChecked());
        param.setBadEgg(false);
        param.setEmptyCage(rbCdEmpty.isChecked());
        param.setEgg_status(eggStatus);
        return param;
    }

    //重置状态
    public void reSetOptions() {
        rbEgg2.setChecked(true);
        rbShouj2.setChecked(true);
        rbCdFd.setChecked(true);
    }

    public void setCpdVisible(int visible) {
        rbEggCpd.setVisibility(visible);
    }

    public void setBfdVisible(int visible) {
        rbBfd.setVisibility(visible);
    }


}
